package Examples;

import guiTesting.RetrieveUserInfo;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.OperationFailedException;
import com.atlassian.crowd.model.user.User;


public class SomeClass extends JFrame{
	List list;
	Object dataValues[][];
	String columnNames[];
	RetrieveUserInfo uInfo; 

	public void initiate() throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException{
		JTable table = getTable();
		JScrollPane pane = new JScrollPane(table);
		getContentPane().add( pane );
		// load table into gui...
	}

	private void createList() throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException
	{
		list = new ArrayList();
		uInfo = new RetrieveUserInfo();
		list = uInfo.searchUsers("sam");   		// populate the list...

	}

	private JTable getTable()	{
		// list must already exist and be populated
		// and must be available in class scope
		// or you can pass it in as an argument
		String columnNames[] = { "FirstName",
				"LastName",
				"Email",
				"DisplayName",
				"Name",
				"Active",
		};
		//System.out.println(list);

		for (int i = 1; i < list.size(); i++){
			System.out.println(list.get(i));
			
			User uza =  (User) list.get(i);
			System.out.println(uza.getFirstName());
			
			dataValues[i][0] = uza.getFirstName(); 
			dataValues[i][1] = uza.getLastName();
			dataValues[i][2] = uza.getEmailAddress();
			dataValues[i][3] = uza.getDisplayName();
			dataValues[i][4] = uza.getName();
			dataValues[i][5] = uza.isActive();
			System.out.println(dataValues[1][0]);
		}
		DefaultTableModel dtm = new DefaultTableModel(dataValues, columnNames);
		JTable table = new JTable(dtm);
		if (table== null){
			System.out.println("booooo");
		}
		return table;
	}
	
	

	public static void main( String[] argv ) throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException {
		
		 SomeClass myExample = new SomeClass("title");
	  }

	  public SomeClass( String title ) throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException {
	    super( title );
	    addWindowListener( new WindowAdapter() {
	      public void windowClosing( WindowEvent we ) {
	        dispose();
	        System.exit( 0 );
	      }
	    } );
	    
	    init();
	    pack();
	    setVisible( true );
	  	}
	  private void init() throws OperationFailedException, InvalidAuthenticationException, ApplicationPermissionException {
		createList(); 
		JScrollPane pane = new JScrollPane(getTable());
		getContentPane().add( pane );
		  
	  }
}
